package KK.Animation
{
	import KK.Time.FrameWheel;
	import KK.Time.IWheel;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point; 
	
	public class MovieClip extends Bitmap implements IWheel
	{ 
		static private var STOP:int=0x0;
		static private var PLAY:int=0x1;
		
		
		
		private var _frameRate:uint;
		private var _frameState:Boolean=true;
		private var _timeWheel:Vector.<int>;
		private var _spriteSheet:SpriteSheet;
		
		private var state:int;
		
		private var sourceBitmapData:BitmapData;
		public function MovieClip(width:int,height:int,bitmapData:BitmapData,frameRate:uint=24)
		{
			//TODO: implement function
			super(bitmapData, "auto", false);
			_frameRate=frameRate; 
			spriteSheet=new SpriteSheet(bitmapData.width,bitmapData.height,width,height,frameRate);
			
			sourceBitmapData=bitmapData;
			setSize(width,height);
			FrameWheel.AddWheel(this);
		}
		
		/**
		 * 添加动画
		 * 
		 */
		public function addAnimation(key:String,list:Array):void{
			_spriteSheet&&_spriteSheet.addAnimation(key,list);
		}
		/**
		 * 移除动画  
		 * 
		 */
		public function removeAnimation(key:String):void{
			_spriteSheet&&_spriteSheet.removeAnimation(key);
		}
		
		/**
		 * 播放动画 
		 * 
		 */
		public function playAnimation(key:String):void{
			_spriteSheet&&_spriteSheet.playAnimation(key);
			Play();
		}
		
		
		/**
		 * 播放  
		 * 
		 */
		public function Play():void{
			state|=PLAY;
		}
		
		/**
		 * 停止 
		 * 
		 */
		public function frameStop():void{
			state=(0xFFFFFFF0&state)|STOP;
			_frameState=false;
		}
		
		/**
		 * 定时器开始  
		 * 
		 */
		public function frameStart():void{
			_frameState=true;
		}
		/**
		 * 获取状态  
		 * @return 
		 * 
		 */
		public function get frameState():Boolean{
			return _frameState;
		}
		
		/**
		 * 获取帧频 
		 * @return 
		 * 
		 */
		public function get frameRate():uint{
			return _frameRate;
		}
		/**
		 * 获取定时区域 
		 * 
		 */
		public function getWheelArea():Vector.<int>{
			return _timeWheel;
		}
		
		/**
		 * 设置定时器区域 
		 * 
		 */
		public function setWheelArea(rect:Vector.<int>):void{
			_timeWheel=rect;
		}
		
		
		/**
		 * 更新函数 
		 * 
		 */
		public function UpdateFrame():void{
			isPlay()&&draw();
		}
		
		
		/**
		 * 重绘 
		 * 
		 */
		protected function draw():void{
			Lightmap();
			_spriteSheet.UpdateFrame();
		}
		
		
		
		/**
		 * 是否播放 
		 * @return 
		 * 
		 */
		private function isPlay():Boolean{
			return (state&0xF)==PLAY;
		}
		
		
		/**
		 * 贴图 
		 * 
		 */
		static private const desPoint:Point=new Point;
		private function Lightmap():void{
			bitmapData.fillRect(bitmapData.rect,0);
			bitmapData.copyPixels(sourceBitmapData,_spriteSheet.Area,desPoint);
		}
		
		/**
		 * 设置大小 
		 * @param width
		 * @param height
		 * 
		 */
		private function setSize(width:int,height:int):void{
			bitmapData=new BitmapData(width,height,true,0);
		}
		
		/**
		 * 设置sheet 
		 * 
		 */
		private function set spriteSheet(value:SpriteSheet):void{
			_spriteSheet=value;
		}
		
	}
}